package com.jf.apply_car.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jf.apply_car.bean.Apply;
import com.jf.apply_car.bean.Userinfo;
import com.jf.apply_car.mapper.ApplyMapper;
import com.jf.apply_car.service.ApplyService;
import com.jf.apply_car.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ApplyServiceImpl implements ApplyService {
    @Autowired
    private ApplyMapper applyMapper;

    //    @Transactional
    public void apply(Apply apply) {
        /*获取当前登录用户*/
        /*封装申请人编号*/
        apply.setApplyUserinfoId(LoginUtil.getLoginInfoId());
        /*根据申请人的角色设置申请状态*/
        if (LoginUtil.getLoginRoleId() == MyConstants.BOSS_ROLE_ID) {
            apply.setApplyStatus(ApplyStatus.BU_MEN_TONG_GUO);
            /*当前用户为负责人时，订单中的部门审核人就是当前用户*/
            apply.setExamDepartmentUserinfoId(LoginUtil.getLoginInfoId());
            apply.setExamDepartmentTime(new Date());
            apply.setExamDepartmentContent("同意");
        } else if (LoginUtil.getLoginRoleId() == MyConstants.EMPLOYEE_ROLE_ID) {
            apply.setApplyStatus(ApplyStatus.YI_SHEN_QING);
        } else {
            throw new BusinessException("权限不足！");
        }
        apply.setApplyTime(new Date());
        applyMapper.insertSelective(apply);
    }

    public ResultJson getMineApplyPage(int page, int limit, Apply apply) {

        apply.setApplyUserinfoId(LoginUtil.getLoginInfoId());

        PageHelper.startPage(page, limit);
        List<Apply> list = applyMapper.getMineApply(apply);
        PageInfo<Apply> pageInfo = new PageInfo<Apply>(list);
        return ResultJson.page(pageInfo.getTotal(), pageInfo.getList());
    }

    public void delete(int[] ids) {
        applyMapper.deleteBatch(ids);
    }

    public ResultJson getAllPage(int page, int limit, Apply apply) {
        Userinfo loginInfo = LoginUtil.getLoginInfo();
        if (loginInfo.getRoleId() == MyConstants.BOSS_ROLE_ID) {
            apply.setDepartmentId(loginInfo.getDepartmentId());
        }
        if (loginInfo.getRoleId() == MyConstants.EMPLOYEE_ROLE_ID) {
            apply.setApplyUserinfoId(loginInfo.getUserinfoId());
        }
        if (loginInfo.getRoleId() == MyConstants.DRIVER_ROLE_ID) {
            apply.setDriverId(loginInfo.getUserinfoId());
        }

        PageHelper.startPage(page, limit);
        List<Apply> list = applyMapper.getAll(apply);
        PageInfo<Apply> pageInfo = new PageInfo<Apply>(list);
        return ResultJson.page(pageInfo.getTotal(), pageInfo.getList());
    }

    public ResultJson getById(int applyId) {
        return ResultJson.success(applyMapper.selectByPrimaryKey(applyId));
    }

    public void exam(Apply apply) {
        int roleId = LoginUtil.getLoginRoleId();
        if (roleId == MyConstants.ADMIN_ROLE_ID) {
            apply.setExamAdminTime(new Date());
            apply.setExamAdminUserinfoId(LoginUtil.getLoginInfoId());
        } else if (roleId == MyConstants.BOSS_ROLE_ID) {
            apply.setExamDepartmentTime(new Date());
            apply.setExamDepartmentUserinfoId(LoginUtil.getLoginInfoId());
        } else {
            throw new BusinessException("权限不足！");
        }
        applyMapper.updateByPrimaryKeySelective(apply);
    }

    public ResultJson getExamPage(int page, int limit, Apply apply) {
        Userinfo loginInfo = LoginUtil.getLoginInfo();
        if (loginInfo.getRoleId() == MyConstants.ADMIN_ROLE_ID) {
            apply.setApplyStatus(ApplyStatus.BU_MEN_TONG_GUO);
        } else if (loginInfo.getRoleId() == MyConstants.BOSS_ROLE_ID) {
            apply.setDepartmentId(loginInfo.getDepartmentId());
            apply.setApplyStatus(ApplyStatus.YI_SHEN_QING);
        } else {
            throw new BusinessException("权限不足！");
        }
        PageHelper.startPage(page, limit);
        List<Apply> list = applyMapper.getAll(apply);
        PageInfo<Apply> pageInfo = new PageInfo<Apply>(list);
        return ResultJson.page(pageInfo.getTotal(), pageInfo.getList());
    }

    public ResultJson getReturnPage(int page, int limit, Apply apply) {
        apply.setApplyStatus(ApplyStatus.GUAN_LI_YUAN_TONG_GUO);
        Userinfo loginInfo = LoginUtil.getLoginInfo();
        if (loginInfo.getRoleId() == MyConstants.BOSS_ROLE_ID || loginInfo.getRoleId() == MyConstants.EMPLOYEE_ROLE_ID) {
            apply.setApplyUserinfoId(loginInfo.getUserinfoId());
        } else if (loginInfo.getRoleId() == MyConstants.DRIVER_ROLE_ID) {
            apply.setDriverId(loginInfo.getUserinfoId());
        } else {
            throw new BusinessException("权限不足！");
        }
        PageHelper.startPage(page, limit);
        List<Apply> list = applyMapper.getAll(apply);
        PageInfo<Apply> pageInfo = new PageInfo<Apply>(list);
        return ResultJson.page(pageInfo.getTotal(), pageInfo.getList());
    }

    public void returnCar(int applyId) {
        Apply apply = new Apply();
        apply.setApplyId(applyId);
        apply.setReturnTime(new Date());
        apply.setReturnUserinfoId(LoginUtil.getLoginInfoId());
        apply.setApplyStatus(ApplyStatus.YI_HUAN_CHE);
        applyMapper.updateByPrimaryKeySelective(apply);
    }

    public void testData(List<Apply> list) {
        applyMapper.insertBatch(list);
    }
}
